package com.t216.dm216.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.t216.dm216.mapper.DmItemMapper;
import com.t216.dm216.pojo.DmCinema;
import com.t216.dm216.pojo.DmItem;
import com.t216.dm216.pojo.DmScheduler;
import com.t216.dm216.pojo.dto.DmItemDTO;
import com.t216.dm216.pojo.dto.DmItemDetailDTO;
import com.t216.dm216.service.DmItemService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
@Service
public class DmItemServiceImpl  implements DmItemService {
    @Resource
    private DmItemMapper dmItemMapper;
    @Override
    public List<DmItem> getBannerItemByItemType1Id(Integer itemType1Id) {
        LambdaQueryWrapper<DmItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DmItem::getItemType1Id, itemType1Id);
        queryWrapper.eq(DmItem::getIsBanner,1);
        /*最后追加sql语句*/
        queryWrapper.last("limit 5");
        return dmItemMapper.selectList(queryWrapper);
    }

    @Override
    public List<DmItem> getRecommendItem() {
        LambdaQueryWrapper<DmItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(DmItem::getId, DmItem::getItemName, DmItem::getMinPrice);
        queryWrapper.eq(DmItem::getIsRecommend,1);
        queryWrapper.last("limit 5");
        return dmItemMapper.selectList(queryWrapper);
    }

    @Override
    public List<DmItemDTO> getFloorItemsByItemType1Id(Integer itemType1Id) {
        /*将每个分类下的前5个数据先查出来*/
        MPJLambdaWrapper<DmItem> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.select(DmItem::getId, DmItem::getItemName, DmItem::getMinPrice);
        queryWrapper.selectAs(DmItem::getStartTime,"startDate");
        queryWrapper.selectAs(DmItem::getEndTime,"endDate");
        queryWrapper.select(DmCinema::getAreaId, DmCinema::getAreaName, DmCinema::getAddress);
        queryWrapper.innerJoin(DmCinema.class,DmCinema::getId,DmItem::getCinemaId);
        queryWrapper.eq(DmItem::getItemType1Id,itemType1Id);
        queryWrapper.last("limit 5");
        return dmItemMapper.selectJoinList(DmItemDTO.class, queryWrapper);
    }

    @Override
    public List<DmItemDTO> getHotItems(Integer itemType1Id) {
        MPJLambdaWrapper<DmItem> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.select(DmItem::getId, DmItem::getItemName);
        queryWrapper.select(DmCinema::getAreaId, DmCinema::getAreaName);
        queryWrapper.leftJoin(DmCinema.class,DmCinema::getId,DmItem::getCinemaId);
        queryWrapper.eq(DmItem::getIsBanner,1);
        queryWrapper.eq(DmItem::getItemType1Id,itemType1Id);
        queryWrapper.last("limit 13");

        return dmItemMapper.selectJoinList(DmItemDTO.class, queryWrapper);
    }

    @Override
    public List<DmItemDTO> getLikeItems(Integer itemTypeId) {
        MPJLambdaWrapper<DmItem> lambdaWrapper = new MPJLambdaWrapper<>();
        lambdaWrapper.select(DmItem::getId, DmItem::getItemName,DmItem::getMinPrice);
        lambdaWrapper.select(DmCinema::getAreaId, DmCinema::getAreaName, DmCinema::getAddress);
        lambdaWrapper.selectAs(DmItem::getStartTime,DmItemDTO::getStartDate);
        lambdaWrapper.selectAs(DmItem::getEndTime,DmItemDTO::getEndDate);
        lambdaWrapper.leftJoin(DmCinema.class,DmCinema::getId,DmItem::getCinemaId);
        lambdaWrapper.eq(itemTypeId != 0,DmItem::getItemType1Id,itemTypeId);
        lambdaWrapper.last("limit 6");

        return dmItemMapper.selectJoinList(DmItemDTO.class, lambdaWrapper);
    }

    @Override
    public DmItemDetailDTO getItemDetailById(Long itemId) {
        MPJLambdaWrapper<DmItem> lambdaWrapper = new MPJLambdaWrapper<>();
        lambdaWrapper.selectAll(DmItem.class);
        lambdaWrapper.select(DmCinema::getAreaId,DmCinema::getAreaName,DmCinema::getAddress);

        lambdaWrapper.leftJoin(DmCinema.class,DmCinema::getId,DmItem::getCinemaId);

        lambdaWrapper.eq(itemId != null && itemId != 0,DmScheduler::getId,itemId);

        return dmItemMapper.selectJoinOne(DmItemDetailDTO.class,lambdaWrapper);
    }

    @Override
    public DmItem getItemById(Long itemId) {
        MPJLambdaWrapper<DmItem> lambdaWrapper = new MPJLambdaWrapper<>();
        lambdaWrapper.select(DmItem::getItemName);
        lambdaWrapper.eq(DmItem::getId,itemId);
        return dmItemMapper.selectOne(lambdaWrapper);
    }
}
